package com.young.shop.admin.controller;

import com.young.shop.admin.service.GoodsService;
import com.young.shop.pojo.entity.Goods;
import com.young.shop.pojo.entity.Page;
import com.young.shop.pojo.vo.BaseRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * 后台商品接口
 */


@Api(tags = "商品业务")
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController {

	@Autowired
	private GoodsService goodsService;


	@ApiOperation("商品上架")
	@PostMapping("/shelves")
	public BaseRespVO shelvesGoodsById(int id) {

		try {
			int shelvesGoods = goodsService.checkShelvesGoods(id);
			return convertSuccessResult(shelvesGoods);
		} catch (RuntimeException e) {
			String errorMsg = e.getMessage();
			return convertErrorResult(errorMsg);
		}
	}


	@ApiOperation("商品下架")
	@PostMapping("/soldOut")
	public BaseRespVO soldOutGoodsById(int id) {

		try {
			int soldOutGoods = goodsService.checkSoldOutGoods(id);
			return convertSuccessResult(soldOutGoods);
		} catch (RuntimeException e) {
			String errorMsg = e.getMessage();
			return convertErrorResult(errorMsg);
		}
	}


	@ApiOperation("商品分页")
	@PostMapping("/byPage")
	public BaseRespVO goodsByPage(Integer status ,
								  Integer currentPage,
								  Integer pageSize,
								  String searchByType) {

		//由于是显示所有的产品信息,没有参数接收
		//需要调用服务层查找所有数据的方法,获取结果,响应给客户端
		try {
			Page page = goodsService.findGoods(status,currentPage, pageSize, searchByType);
			//System.out.println("进入分页查询："+status);
			return convertSuccessResult(page);

		} catch (RuntimeException e) {
			String errorMsg = e.getMessage();
			return convertErrorResult(errorMsg);
		}
	}



	@ApiOperation("文件上传")
	@PostMapping(value = "/upload", consumes = "multipart/*",
			headers = "content-type=multipart/form-data")
	public String addGoods(@ApiParam(value = "文件") MultipartFile file, HttpServletResponse response) {

		response.setHeader("Content-Type", "multipart/form-data");
		if (file.isEmpty()) {
			throw new RuntimeException("文件上传失败");
		}
		String filePath = "E:\\Java-Study_Projects\\shop\\gitbug-shop\\views\\view-admin\\img\\"
				+ file.getOriginalFilename();
		//String filePath = "C:\\Users\\MG.MX\\Desktop\\test\\" + file.getOriginalFilename();
		System.out.println("upload:" + filePath);

		String filename = file.getOriginalFilename();
		File saveFile = new File(filePath);
		try {
			file.transferTo(saveFile);
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println("imgUrl："+filename);
		return filename;


	}
//goods.setImgUrl("http://localhost:8080/view-admin/img/" + file.getOriginalFilename());


	@ApiOperation("新增商品")
	@PostMapping("/addGoods")
	public BaseRespVO addGoods(Goods goods) {

		try {

			System.out.println("goods" + goods);
			goods.setImgUrl("http://localhost:8080/views/view-admin/img/" + goods.getImgUrl());
			int addGoods = goodsService.addGoods(goods);

			return convertSuccessResult(addGoods);
		} catch (RuntimeException e) {
			String errorMsg = e.getMessage();
			return convertErrorResult(errorMsg);
		}
	}


	@ApiOperation("根据状态查询商品列表")
	@PostMapping("/delete")
	public BaseRespVO delGoods(int id){

		try {
			int goods = goodsService.deleteGoods(id);
			return convertSuccessResult(goods);
		}catch (RuntimeException e){
			String errorMsg = e.getMessage();
			return convertErrorResult(errorMsg);
		}
	}










	/*@ApiOperation("获取搜有商品")
	@GetMapping("/selectAllGoods")
	public BaseRespVO selectAllGoods() {

		try {
			List<Goods> goodsList = goodsService.getAllGoods();
			return convertSuccessResult(goodsList);
		} catch (RuntimeException e) {
			String errorMsg = e.getMessage();
			return convertErrorResult(errorMsg);
		}
	}*/


	/*
	@ApiOperation("搜索商品类型分页")
	@PostMapping("/searchGoodsByType")
	public BaseRespVO goodsTypeByPage(String searchByType) {

		try {
			if (searchByType == null) {
				List<Goods> allGoods = goodsService.getAllGoods();
				return convertSuccessResult(allGoods);
			}
			List<Goods> goodsList = goodsService.selectGoodsListByType(searchByType);
			return convertSuccessResult(goodsList);

		} catch (RuntimeException e) {
			String errorMsg = e.getMessage();
			return convertErrorResult(errorMsg);
		}
	}*/


	/*
	@ApiOperation("根据状态查询商品列表")
	@PostMapping("/goodsByStatus")
	public BaseRespVO goodsStatus(int status,int currentPage,int pageSize){

		try {
			Page page = goodsService.goodsByStatus(status,currentPage,pageSize);
			return convertSuccessResult(page);
		}catch (RuntimeException e){
			String errorMsg = e.getMessage();
			return convertErrorResult(errorMsg);
		}
	}*/


}
